/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://github.com/mineadmin
 */
import type { MaProTableColumns, MaProTableExpose } from "@mineadmin/pro-table";
import type { UseDialogExpose } from "@/hooks/useDialog.ts";

import defaultAvatar from "@/assets/images/defaultAvatar.jpg";
import { ElTag } from "element-plus";
import { useMessage } from "@/hooks/useMessage.ts";
import { ResultCode } from "@/utils/ResultCode.ts";
import hasAuth from "@/utils/permission/hasAuth.ts";
import { keyBy, get } from "lodash-es";
import { deleteByIds } from "@/plugins/ai-warning/organization/api/rdOrganizeArea.ts";
import { rdOrganizeAreaStatusDictData } from "./common.tsx";

export default function getTableColumns(
  dialog: UseDialogExpose,
  formRef: any,
  t: any
): MaProTableColumns[] {
  const rdOrganizeAreaStatusMap = keyBy(
    rdOrganizeAreaStatusDictData(),
    "value"
  );
  const msg = useMessage();

  return [
    {
      type: "selection",
      showOverflowTooltip: false,
      label: () => t("crud.selection"),
    },
    {
      label: "所属项目",
      prop: "proj_line.name",
      showOverflowTooltip: true,
      cellRender: ({ row }) => {
        return row.proj_line?.name || "-";
      },
    },
    {
      label: "工区名称",
      prop: "name",
      showOverflowTooltip: true,
    },
    {
      label: "简称",
      prop: "abbr",
      showOverflowTooltip: true,
    },
    {
      label: "地址",
      prop: "addr",
      showOverflowTooltip: true,
      width: 200,
    },
    {
      label: "坐标",
      prop: "coordinate",
      width: 150,
      cellRender: ({ row }) => {
        if (row.lat && row.lng) {
          return `${row.lat}, ${row.lng}`;
        }
        return "-";
      },
    },
    {
      label: "排序",
      prop: "sort",
      width: 80,
      align: "center",
    },
    {
      label: "状态",
      prop: "status",
      width: 100,
      align: "center",
      cellRender: ({ row }) => {
        const statusInfo = get(rdOrganizeAreaStatusMap, row.status, {
          label: "未知",
          value: row.status,
        });
        const tagType =
          row.status === 1 ? "success" : row.status === 0 ? "danger" : "info";
        return <ElTag type={tagType}>{statusInfo.label}</ElTag>;
      },
    },
    {
      label: "摘要",
      prop: "abstract",
      showOverflowTooltip: true,
      width: 150,
    },
    {
      label: "创建时间",
      prop: "created_at",
      width: 180,
      showOverflowTooltip: true,
    },
    {
      label: "更新时间",
      prop: "updated_at",
      width: 180,
      showOverflowTooltip: true,
    },
    {
      type: "operation",
      label: () => t("crud.operation"),
      fixed: "right",
      width: 120,
      operationConfigure: {
        actions: [
          {
            name: "edit",
            icon: "material-symbols:person-edit",
            show: ({ row }) => hasAuth("organization:rdOrganizeArea:update"),
            text: () => t("crud.edit"),
            onClick: ({ row }) => {
              dialog.setTitle(t("crud.edit"));
              dialog.open({ formType: "edit", data: row });
            },
          },
          {
            name: "del",
            icon: "mdi:delete",
            text: () => t("crud.delete"),
            show: ({ row }) => hasAuth("organization:rdOrganizeArea:delete"),
            onClick: ({ row }, proxy: MaProTableExpose) => {
              msg.delConfirm(t("crud.delDataMessage")).then(async () => {
                const response = await deleteByIds([row.id]);
                if (response.code === ResultCode.SUCCESS) {
                  msg.success(t("crud.delSuccess"));
                  proxy.refresh();
                }
              });
            },
          },
        ],
      },
    },
  ];
}
